package enge.checkers;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

/**
 * // -------------------------------------------------------------------------
 * /** This class is required by android apps to set up the application.
 * 
 * @author Kyle Parker (kyle93)
 * @author Blake Whaley
 * @author Ianjvb
 * @version Apr 25, 2012
 */
public class CheckersActivity
    extends Activity
    implements OnTouchListener
{
    private CheckerBoard board;
    private CheckerView  boardView;
    private TextView     statusLabel;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        boardView = (CheckerView)findViewById(R.id.checkerView1);
        board =
            new CheckerBoard(
                Color.BLACK,
                Color.WHITE,
                PlayerType.ONE,
                PlayerType.TWO);
        boardView.setBoard(board);

        statusLabel = (TextView)findViewById(R.id.statusLabel);
        statusLabel.setText("You must click end turn after jumping a piece");
    }


    public void pushNewGame(View view)
    {
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }


    public void pushEndTurn(View view)
    {
        board.setJumped(false);
        if (board.twoPlayer)
        {
        	board.changeTurn();
        }
        else
        	board.computerMove();
    }
    
    public void pushTwoPlayer(View view)
    {
    	if (board.getTwoPlayer() == true)
    	{
    		board.setTwoPlayer(false);
    	}
    	else
    		board.setTwoPlayer(true);
    }


    public boolean onTouch(View v, MotionEvent event)
    {
        if (board.checkGameOver())
        {
            if (board.getCounter1() == 0 || !board.canPlayer1Move())
            {
                statusLabel.setText("GAME OVER! PLAYER 1 HAS LOST!");
                statusLabel.postInvalidate();
            }
            else
            {
                statusLabel.setText("GAME OVER! PLAYER 2 HAS LOST!");
                statusLabel.postInvalidate();
            }

        }
        boardView.invalidate();
        return true;
    }
}
